package leetcode;

import java.util.Stack;

public class jiubaisishiliu {
    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack=new Stack<>();
        int j=0;
        for(int i=0;i<pushed.length;i++){
            stack.push(pushed[i]);
            while(stack.peek()==popped[j]){
                stack.pop();
                if(stack.isEmpty()){
                    if(j==pushed.length-1){
                        return true;
                    }
                    j++;
                    stack.push(pushed[i+1]);
                    i=i+1;
                }else{
                    j++;
                }
            }
        }
        return false;

    }

    public static void main(String[] args) {
        int[] pushed={4,0,1,2,3};
        int[] popped={4,2,3,0,1};
//        int[] pushed={0,2,1};
//        int[] popped={0,1,2};
        boolean b = validateStackSequences(pushed, popped);
        System.out.println(b);

    }
}
